home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1997
/
MacHack 1997.toast
/
Hacks
/
Hacks ’93
/
String Extractor⁄Localization
/
Scanner
/
Literal.c
< prev
next >
Wrap
Text File
|
1993-06-18
|
3KB
|
143 lines
/*
* Literal.c
*/
#include "Literal.h"
#define DEFAULT_SIZE 8
typedef struct MajorLine {
short id ;
short numStrings ;
char * * stringData ;
char * * stringStarts ;
} MajorLine ;
MajorLine * cache = NULL ;
short cacheSize = 0 ;
short cacheBase = 0 ;
static void
LoadID ( short id , MajorLine * where ) {
Handle h ;
unsigned char * iterator ;
unsigned char * from ;
short count ;
if ( where -> stringData ) {
DisposeHandle ( where -> stringData ) ;
}
if ( where -> stringStarts ) {
DisposePtr ( ( Ptr ) where -> stringStarts ) ;
}
h = GetResource ( 'STR#' , id ) ;
DetachResource ( h ) ;
where -> numStrings = * ( short * ) * h ;
where -> stringStarts = ( char * * ) NewPtrClear ( sizeof ( char * ) *
where -> numStrings ) ;
where -> id = id ;
SetHandleSize ( h , GetHandleSize ( h ) + where -> numStrings ) ;
HLockHi ( h ) ;
where -> stringData = h ;
count = 0 ;
iterator = ( unsigned char * ) * h + 2 ;
while ( count < where -> numStrings ) {
where -> stringStarts [ count ] = ( char * ) iterator ;
iterator += * iterator + 1 ;
count ++ ;
}
from = iterator - 1 ;
iterator += count - 1 ; /* Last in buffer */
while ( count -- ) {
int cnt = ( * ( unsigned char * ) where -> stringStarts [ count ] ) + 1 ;
* ( iterator -- ) = 0 ;
while ( cnt -- ) {
* ( iterator -- ) = * ( from -- ) ;
}
where -> stringStarts [ count ] += count ;
}
}
static Boolean
InitCache ( short size ) {
if ( cache ) {
SysBeep ( 20 ) ;
return 0 ;
}
cacheSize = size ;
cache = ( MajorLine * ) NewPtrClear ( sizeof ( MajorLine ) * size ) ;
return 1 ;
}
void
InitStringTable ( short id ) {
short count , idSave = id , pos ;
Handle h ;
cacheBase = id ;
if ( cache ) {
SysBeep ( 20 ) ;
return ;
}
SetResLoad ( 0 ) ;
while ( h = GetResource ( 'STR#' , id ) ) {
ReleaseResource ( h ) ;
id ++ ;
count ++ ;
}
SetResLoad ( 1 ) ;
InitCache ( count ) ;
for ( id = idSave ; id < idSave + count ; id ++ ) {
pos = ( ( id - cacheBase ) & 0x7fff ) % cacheSize ;
LoadID ( id , cache + pos ) ;
}
}
char *
CLiteral ( short id , short num ) {
short pos ;
if ( ! cache ) {
if ( ! InitCache ( DEFAULT_SIZE ) ) {
return NULL ;
}
}
pos = ( ( id - cacheBase ) & 0x7fff ) % cacheSize ;
if ( ! ( cache [ pos ] . id == id ) ) {
LoadID ( id , cache + pos ) ;
}
if ( num < 1 || num > cache [ pos ] . numStrings ) {
return NULL ;
}
return cache [ pos ] . stringStarts [ num - 1 ] + 1 ; /* C string */
}
unsigned char *
PLiteral ( short id , short num ) {
unsigned char * ret = ( unsigned char * ) CLiteral ( id , num ) ;
if ( ret ) {
return ret - 1 ; /* Adjust for pascal string */
}
return ret ;
}